package etherum

import (
	block_chain "gitee.com/perception-network/block-chain-released/pkg/blockChain"
	"gitee.com/perception-network/block-chain-released/pkg/parseGithubReleased"
	"time"
)

const (
	ethName     = "etherum"
	projectName = "etherum"
	url         = "https://github.com/ethereum/go-ethereum/releases/latest"
)

type eth struct {
	version string
}

func init() {
	block_chain.AddBlockChain(ethName, newEth())
}

func newEth() *eth {
	return &eth{}
}

func (e *eth) RunWithChan(bc chan<- block_chain.BlockChainMsg) {
	go func() {
		e.getVersion(bc)
		tc := time.NewTicker(time.Minute)
		for range tc.C {
			e.getVersion(bc)
		}
	}()
}

func (e *eth) getVersion(bc chan<- block_chain.BlockChainMsg) {
	version, _ := parseGithubReleased.GetCurrentInfo(url)
	if version != "" {
		//if e.version != version {
		e.version = version
		bc <- block_chain.BlockChainMsg{
			Name:    ethName,
			Project: projectName,
			Version: version,
			Repo:    url,
		}
		//}
	}
}
